/*--------------------权限模块.用户管理--ly-----------------*/
/*--------------------（管理员权限）用户信息修改功能 user_edit.html------------------*/

$(document).ready(function() {

	//获取cookie:ueserid & Wtid
	var userid = $.cookie("userid");
	console.log(userid);
	var editcookie = $.cookie("nav");
	var Wtid = JSON.parse(editcookie).w;

	//填充数据
	var searchurl = jurisdictionport + '/UserController/selectByPrimaryKey'; //查询
	$.get(searchurl, {
		'userid': userid
	}, function(result) {
		console.log(result);
		var code = result.code;
		var editlist = result.object;
		if(code == 0) {
			$("#L_username").val(editlist.username); //用户名
			$("#L_email").val(editlist.email); //邮箱
			$("#L_mobile").val(editlist.mobile); //手机号码
			$("#L_realname").val(editlist.realname); //真实姓名
			$("#L_wxid").val(editlist.wxid); //微信公众号
			$("#L_qrcode").val(editlist.qrcode); //二维码地址
			$("#L_password").val(editlist.password); //密码
		} else {
			console.log("网络不佳，稍后再试！")
		}
	});

	//layui
	layui.use(['form', 'layer'], function() {
		$ = layui.jquery;
		var form = layui.form,
			layer = layui.layer;

		//自定义验证规则
		form.verify({
			username: function(value) {
				if(value.length < 5) {
					return '昵称至少得5个字符啊';
				}
			},
			password: [/(.+){6,12}$/, '密码必须6到12位'],
			// repass: function (value) {
			//     if ($('#L_pass').val() != $('#L_repass').val()) {
			//         return '两次密码不一致';
			//     }
			// },
			id_card: function(value) {
				if(value.length != 18) {
					return '请输入正确的18位身份证号码';
				}
			}
		});

		//获取表单数据
		$("input").blur(function() {
			//失去焦点事件
			var username = $("#L_username").val(); //用户名
			var email = $("#L_email").val(); //邮箱
			var mobile = $("#L_mobile").val(); //手机号码
			var realname = $("#L_realname").val(); //真实姓名
			var wxid = $("#L_wxid").val(); //微信公众号
			var qrcode = $("#L_qrcode").val(); //二维码地址
			var password = $("#L_password").val(); //密码
			console.log(username, email, mobile, realname, wxid, qrcode, password);
			//监听提交
			form.on('submit(edit)', function() {
				//递交修改
				editData(username, email, mobile, realname, wxid, qrcode, password);
				return false;
			});
		});

		//修改用户信息
		var editurl = jurisdictionport + '/UserController/modifyByUserid'; //修改
		// 获得frame索引
		var index = parent.layer.getFrameIndex(window.name);
		var editData = function(username, email, mobile, realname, wxid, qrcode, password) {
			//修改 edit
			$.get(editurl, {
				userid: userid,
				username: username,
				email: email,
				mobile: mobile,
				realname: realname,
				wxid: wxid,
				qrcode: qrcode,
				password: password
			}, function(result) {
				var code = result.code;
				if(code == 0) {
					layer.msg('修改成功!', {
						icon: 1,
						time: 1000
					}, function() {
						//关闭当前frame
						parent.layer.close(index);
					});
				} else {
					layer.msg('网络开小差了，请稍后再试!', {
						icon: 2,
						time: 1000
					}, function() {
						//关闭当前frame
//						parent.layer.close(index);
					});
				}
			})
		}

	});
})